package com.funny.learning.english.activity;

import java.util.ArrayList;
import java.util.Arrays;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.funny.learning.english.BaseActivity;
import com.funny.learning.english.R;

public class SettingActivity extends BaseActivity implements OnClickListener {
	private ImageView ivPause;
	private Spinner spLang;
	private Spinner spNotify;
	private Spinner spReNotity;
	
	
	
	ArrayList<String> arrLang = new ArrayList<String>(Arrays.asList("Tiếng Anh","Tiếng Việt"));
	ArrayList<String> arrTimeNotify = new ArrayList<String>(Arrays.asList("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"));
	ArrayList<String> arrTimeReNotify = new ArrayList<String>(Arrays.asList("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"));
	private TextView tvSpLang;
	private TextView tvSpNotify;
	private TextView tvSpReNotity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_layout);
		
		spLang = (Spinner)findViewById(R.id.spLang);
		spNotify = (Spinner)findViewById(R.id.spNotify);
		spReNotity = (Spinner)findViewById(R.id.spReNotity);
		tvSpLang = (TextView)findViewById(R.id.tvSpLang);
		tvSpNotify = (TextView)findViewById(R.id.tvSpNotify);
		tvSpReNotity = (TextView)findViewById(R.id.tvSpReNotity);

		ivPause = (ImageView) findViewById(R.id.ivPause);
		ivPause.setVisibility(View.GONE);
		
		initSpinner(spLang, arrLang, tvSpLang, 3);
		initSpinner(spNotify, arrTimeNotify, tvSpNotify, 1);
		initSpinner(spReNotity, arrTimeReNotify, tvSpReNotity, 2);
		
		tvSpLang.setOnClickListener(this);
		tvSpNotify.setOnClickListener(this);
		tvSpReNotity.setOnClickListener(this);
	}
	
	
	
	private void initSpinner(Spinner spinner, final ArrayList<String> arrData, final TextView textView, final int mode){
		ArrayAdapter<String> mSpTopicArrayAdapter = new ArrayAdapter<String>(
				this, android.R.layout.simple_spinner_item, arrData);
		mSpTopicArrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(mSpTopicArrayAdapter);
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				
				String strSelect = arrData.get(position);
				
				if(mode == 1){
					int hour = Integer.valueOf(strSelect);
					if(hour <= 12){
						textView.setText(hour + "h AM");
					}else{
						if(hour == 13){
							hour = 1;
						}else if(hour == 14){
							hour = 2;
						}else if(hour == 15){
							hour = 3;
						}else if(hour == 16){
							hour = 4;
						}else if(hour == 17){
							hour = 5;
						}else if(hour == 18){
							hour = 6;
						}else if(hour == 19){
							hour = 7;
						}else if(hour == 20){
							hour = 8;
						}else if(hour == 21){
							hour = 9;
						}else if(hour == 22){
							hour = 10;
						}else if(hour == 23){
							hour = 11;
						}else if(hour == 24){
							hour = 12;
						}
						textView.setText(hour + "h PM");
					}
				}else if(mode == 2){
					textView.setText(strSelect + "h");
				}else{
					textView.setText(strSelect);
				}
				
				
			}
			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});
	}



	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.tvSpLang:
			spLang.performClick();
			break;
		case R.id.tvSpNotify:
			spNotify.performClick();
			break;
		case R.id.tvSpReNotity:
			spReNotity.performClick();
			break;

		default:
			break;
		}
	}

}
